<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { permissionApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const $emit = defineEmits(['updateList'])
  const formLabelWidth = '80px';
  const dataForm = ref()
  const action = reactive({
    dialogFormVisible:false,
    parents:[] as any[]
  })
  interface formQuery {
    id:number
    pid:number
    name:string
    desc:string
  }
  let form = reactive({} as formQuery)
  const formRules = {
    pid: [{ required: true, trigger: 'blur', message: '父级不可为空' }],
    name: [{ required: true, trigger: 'blur', message: '权限名不可为空' }],
    desc: [{ required: true, trigger: 'blur', message: '描述不可为空' }]
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid: any)=>{
      if(valid) {
        const loading = Toastloading();
        if(form.id) {
          await permissionApi.updatePermission(form.id,form)
          ToastSuccess('修改成功')
        }else {
          await permissionApi.addPermission(form)
          ToastSuccess('新增成功')
        }
        loading.close();
        closeDialog();
        $emit('updateList')
      }
    })
  }
  const getParentsList = async()=> {
    const response = await permissionApi.getParents();
    const { data = [] } = response
    action.parents = data
  }
  const openDialog = async(query:any = {})=> {
    const loading = Toastloading()
    await getParentsList();
    Object.assign(form,query)
    loading.close()
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuery)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="false"
    :show-close="false"
    :close-on-press-escape="false"
    :title="form.id?'修改权限':'新增权限'">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="父级" prop="pid">
          <el-select v-model="form.pid" filterable placeholder="父级">
            <el-option label="根" :value="0"/>
            <el-option v-for="(parent,key) of action.parents" :key="key" :label="parent.desc" :value="parent.id"/>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="权限名" prop="name">
          <el-input v-model="form.name" :autofocus="true" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述" prop="desc">
          <el-input v-model="form.desc"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>